source 'movie.rb'

module Enumerable
  def group_by
    h = {}
    for item in self
      key = yield item
      h[key] = [] unless h[key]
      h[key].push(item)
    end
    return h
  end
end

### Higher-order functions ###

# 1
a = %w(apple banana cherry alice bob carol alpha bravo charlie)
a.group_by { |x| x[0,1].to_s }  # x[0,1] if x is a string returns a string representing the first character of x
class String ; def first_letter ; self[0,1].to_s ; end ; end
a.group_by { |x| x.first_letter }  # should give same results as first example

# 3.1
#movies.collect { |m| m.showtime.strftime("%H%M") > "1700" ? m : nil } .compact!
movies.select { |m| m.showtime.strftime("%H%M") > "1700" }

# 3.2
movies.select { |m| m.showtime.strftime("%H%M") > "1700" }.length

# 3.3
movies.select { |m| m.showtime.strftime("%H%M") > "1700" }.collect! { |m| m.name }.uniq
